//
//  SDHSportsMatchHeaderView.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/5.
//

#import "SDHSportsMatchHeaderView.h"
#import "SDHSportsMatchHeaderButton.h"

static const CGFloat kHeaderBtnHeight = 80;
static const NSInteger btnIndexTag = 1001;

@interface SDHSportsMatchHeaderView ()

@property (nonatomic, strong) NSMutableArray *arrBtns;

@end

@implementation SDHSportsMatchHeaderView

- (id)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor blackColor];
        _arrBtns = [NSMutableArray array];
        
        SDHSportsMatchHeaderButton *btn =  [[[NSBundle mainBundle] loadNibNamed:@"SDHSportsMatchHeaderButton" owner:nil options:nil] lastObject];
        btn.btnImg.image = [UIImage imageNamed:@"zuqiu"];
        btn.lbName.text = @"Soccer";
        [self addSubview:btn];
        btn.tag = btnIndexTag + 1;
        
        SDHSportsMatchHeaderButton *btn2 =  [[[NSBundle mainBundle] loadNibNamed:@"SDHSportsMatchHeaderButton" owner:nil options:nil] lastObject];
        btn2.btnImg.image = [UIImage imageNamed:@"lanqiu"];
        btn2.lbName.text = @"Basketball";
        [self addSubview:btn2];
        btn2.tag = btnIndexTag + 2;
        
        SDHSportsMatchHeaderButton *btn3 =  [[[NSBundle mainBundle] loadNibNamed:@"SDHSportsMatchHeaderButton" owner:nil options:nil] lastObject];
        btn3.btnImg.image = [UIImage imageNamed:@"bangqiu"];
        btn3.lbName.text = @"Baseball";
        [self addSubview:btn3];
        btn3.tag = btnIndexTag + 3;
        
        SDHSportsMatchHeaderButton *btn4 =  [[[NSBundle mainBundle] loadNibNamed:@"SDHSportsMatchHeaderButton" owner:nil options:nil] lastObject];
        btn4.btnImg.image = [UIImage imageNamed:@"wangqiu"];
        btn4.lbName.text = @"Tennis";
        [self addSubview:btn4];
        btn4.tag = btnIndexTag + 4;
        
        __weak typeof(self) weakSelf = self;
        btn.btnClick = ^(NSInteger index) {
            [weakSelf titleClick:index];
        };
        btn2.btnClick = ^(NSInteger index) {
            [weakSelf titleClick:index];
        };
        btn3.btnClick = ^(NSInteger index) {
            [weakSelf titleClick:index];
        };
        btn4.btnClick = ^(NSInteger index) {
            [weakSelf titleClick:index];
        };
        _selectedIndex = 0;
        btn.isSelected = YES;
        btn2.isSelected = NO;
        btn3.isSelected = NO;
        btn4.isSelected = NO;
        
        [_arrBtns removeAllObjects];
        [_arrBtns addObject:btn];
        [_arrBtns addObject:btn2];
        [_arrBtns addObject:btn3];
        [_arrBtns addObject:btn4];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).offset(20);
            make.left.equalTo(self.mas_left).offset(15);
            make.height.equalTo(@(kHeaderBtnHeight));
            make.right.equalTo(btn2.mas_left).offset(-10);
            make.width.equalTo(btn2.mas_width);
        }];
        
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_top);
            make.left.equalTo(btn.mas_right).offset(10);
            make.height.equalTo(@(kHeaderBtnHeight));
            make.right.equalTo(btn3.mas_left).offset(-10);
            make.width.equalTo(btn.mas_width);
        }];
        
        [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_top);
            make.left.equalTo(btn2.mas_right).offset(10);
            make.height.equalTo(@(kHeaderBtnHeight));
            make.right.equalTo(btn4.mas_left).offset(-10);
            make.width.equalTo(btn.mas_width);
        }];
        
        [btn4 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_top);
            make.left.equalTo(btn3.mas_right).offset(10);
            make.height.equalTo(@(kHeaderBtnHeight));
            make.right.equalTo(self).offset(-15);
            make.width.equalTo(btn.mas_width);
        }];
    }
    
    return self;
}

- (void)titleClick:(NSInteger)flag {
    for (SDHSportsMatchHeaderButton *btn in _arrBtns) {
        if (btn.tag == flag) {
            btn.isSelected = YES;
            _selectedIndex = btn.tag - btnIndexTag - 1;
        } else {
            btn.isSelected = NO;
        }
    }
    
    if (self.headerClick) {
        self.headerClick(_selectedIndex);
    }
}

@end
